

#import "YDBaseNavigationController.h"
#import "UIViewController+SYNavigationExtion.h"

//全局返回图片
#define kDefaultBackImageName @"nav-all-back"

#pragma mark  SYWrapNavigationController

@interface SYWrapNavigationController  : UINavigationController

@end
@implementation SYWrapNavigationController

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    return [self.navigationController popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
{
    return [self.navigationController popToRootViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    YDBaseNavigationController *drj_navigationController = viewController.drj_navigationController;
    NSInteger index = [drj_navigationController.drj_viewControllers indexOfObject:viewController];
    return [self.navigationController popToViewController:drj_navigationController.viewControllers[index] animated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    viewController.drj_navigationController = (YDBaseNavigationController *)self.navigationController;
    
    viewController.drj_fullScreenPopGestureEnabled = viewController.drj_navigationController.fullScreenPopGestureEnabled;
    
    UIImage *backButtonImage = viewController.drj_navigationController.backButtonImage;
    
    if (!backButtonImage) {
        backButtonImage = [UIImage imageWIthOriginalImage:kDefaultBackImageName];
    }
    
    //告诉那个界面点击返回键，直接返回跟控制器
    if ([viewController isKindOfClass:NSClassFromString(@"YDRechargeSuccessViewController")] ||
       [viewController isKindOfClass:NSClassFromString(@"YDWithdrawalSuccessViewController")] ) {
       
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(popToRootViewController)];
        
        
    }else{
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(didTapBackButton)];
    }

    //告诉哪个界面取消全局滑动返回
    if ([viewController isKindOfClass:NSClassFromString(@"BYQBuyViewController")] ||
        [viewController isKindOfClass:NSClassFromString(@"YDRechargeSuccessViewController")] ||
        [viewController isKindOfClass:NSClassFromString(@"YDWithdrawalSuccessViewController")]||
        [viewController isKindOfClass:NSClassFromString(@"YDAdvertViewController")]||
        [viewController isKindOfClass:NSClassFromString(@"YDFreeAdvertViewController")]) {
        viewController.drj_fullScreenPopGestureEnabled = NO;
    }

    
    
    
    [self.navigationController pushViewController:[SYWrapViewController wrapViewControllerWithViewController:viewController] animated:animated];
}

- (void)didTapBackButton
{

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popToRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [self.navigationController dismissViewControllerAnimated:flag completion:completion];
    self.viewControllers.firstObject.drj_navigationController=nil;
}

@end
#pragma mark  SYWrapViewController
static NSValue *drj_tabBarRectValue;


@interface SYWrapViewController ()
@property (nonatomic, copy) NSString *className;
@end

@implementation SYWrapViewController

+ (SYWrapViewController *)wrapViewControllerWithViewController:(UIViewController *)viewController
{
    
    SYWrapNavigationController *wrapNavController = [[SYWrapNavigationController alloc] init];
    wrapNavController.viewControllers = @[viewController];
    SYWrapViewController *wrapViewController = [[SYWrapViewController alloc] init];
    [wrapViewController.view addSubview:wrapNavController.view];
    [wrapViewController addChildViewController:wrapNavController];
    return wrapViewController;
}


-(NSString *)className
{
    return  NSStringFromClass([self rootViewController].class);
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if (self.tabBarController && !drj_tabBarRectValue) {
        drj_tabBarRectValue = [NSValue valueWithCGRect:self.tabBarController.tabBar.frame];
    }
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    if (self.tabBarController && [self rootViewController].hidesBottomBarWhenPushed) {
        self.tabBarController.tabBar.frame = CGRectZero;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tabBarController.tabBar.translucent = YES;
    if (self.tabBarController && !self.tabBarController.tabBar.hidden && drj_tabBarRectValue) {
        self.tabBarController.tabBar.frame = drj_tabBarRectValue.CGRectValue;
    }
}

- (BOOL)drj_fullScreenPopGestureEnabled
{
    return [self rootViewController].drj_fullScreenPopGestureEnabled;
}

- (BOOL)hidesBottomBarWhenPushed
{
    return [self rootViewController].hidesBottomBarWhenPushed;
}

- (UITabBarItem *)tabBarItem
{
    return [self rootViewController].tabBarItem;
}

- (NSString *)title
{
    return [self rootViewController].title;
}

- (UIViewController *)childViewControllerForStatusBarStyle
{
    return [self rootViewController];
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return [self rootViewController];
}

- (UIViewController *)rootViewController
{
    SYWrapNavigationController *wrapNavController = self.childViewControllers.firstObject;
    return wrapNavController.viewControllers.firstObject;
}

@end

#pragma mark YDBaseNavigationController
@interface YDBaseNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>


@property (nonatomic, strong) UIPanGestureRecognizer *popPanGesture;

@property (nonatomic, strong) id popGestureDelegate;

@end

@implementation YDBaseNavigationController

+(void)load
{
    //获取当前导航控制器的UINavigationBar
    //通过富文本属性修改导航跳上内容的属性
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //导航栏字体带下及颜色
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
    dict[NSForegroundColorAttributeName] = NumberColor(255, 255, 255);
    bar.titleTextAttributes = dict;

    //导航条的背景图
    [bar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0E4483"]] forBarMetrics:UIBarMetricsDefault];
    

}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
    if (self = [super init]) {
        rootViewController.drj_navigationController = self;
        self.viewControllers = @[[SYWrapViewController wrapViewControllerWithViewController:rootViewController]];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        self.viewControllers.firstObject.drj_navigationController = self;
        self.viewControllers = @[[SYWrapViewController wrapViewControllerWithViewController:self.viewControllers.firstObject]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavigationBarHidden:YES];
    
    self.delegate = self;
    self.popGestureDelegate = self.interactivePopGestureRecognizer.delegate;
    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    self.popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popGestureDelegate action:action];
    self.popPanGesture.maximumNumberOfTouches = 1;
}


#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    BOOL isRootVC = viewController == navigationController.viewControllers.firstObject;

    if (viewController.drj_fullScreenPopGestureEnabled) {
        if (isRootVC) {
            [self.view removeGestureRecognizer:self.popPanGesture];
        } else {
            [self.view addGestureRecognizer:self.popPanGesture];
        }
        self.interactivePopGestureRecognizer.delegate = self.popGestureDelegate;
        self.interactivePopGestureRecognizer.enabled = NO;
    } else {
        [self.view removeGestureRecognizer:self.popPanGesture];
        self.interactivePopGestureRecognizer.delegate = self;
        self.interactivePopGestureRecognizer.enabled = !isRootVC;
    }

    //取消全屏左滑手势
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        if ([((SYWrapViewController *)viewController).className isEqualToString:@"YDRechargeSuccessViewController"] ||
            [((SYWrapViewController *)viewController).className isEqualToString:@"YDWithdrawalSuccessViewController"]||
             [((SYWrapViewController *)viewController).className isEqualToString:@"YDAdvertViewController"]||
             [((SYWrapViewController *)viewController).className isEqualToString:@"YDFreeAdvertViewController"]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
#pragma mark - Getter

- (NSArray *)drj_viewControllers
{
    NSMutableArray *viewControllers = [NSMutableArray array];
    for (SYWrapViewController *wrapViewController in self.viewControllers) {
        [viewControllers addObject:wrapViewController.rootViewController];
    }
    return viewControllers.copy;
}

@end

